$(function(){
	var $panel=navTab.getCurrentPanel();
	panelLoaded($panel.children(".page"));
})

function showChildren(data, $dom){
	var tag=$("#"+$dom.attr("rel"));
	var oVal = $dom.text();
	var cells=$dom.parents("table.list")[0].rows.item(1).cells.length;
	tag.attr("colspan",cells);
	
	$dom.html("<span><img src='./themes/default/images/form/attach2.gif'></span>");
	$dom.unbind("click").removeAttr("href").toggle(function(){
		//$(this).text(oVal);
		$(this).html("<span><img src='./themes/default/images/form/attach.gif'></span>");
		tag.parent().hide();
	},function(){
		//$(this).text("收起");
		$(this).html("<span><img src='./themes/default/images/form/attach2.gif'></span>");
		tag.parent().show();
	});
	
}
function updateEntityItem(item,$dom){
	var table=$dom.parents("table.list").eq(0);
	var entity=$("tr[entity="+item.entity_id+"]",table);
	entity.find("td[name]").each(function(){
		var $this=$(this),name=$this.attr("name");
		$this.text(item[name]||"");
	});
	if(item["ontop"]==0){
		entity.find("td[name=title]").attr("class","ontop0");
	}else{
		entity.find("td[name=title]").attr("class","ontop1");
	}
	alertMsg.correct("更新成功");
}

function uploadEntityPic(msg,$dom){
		$("img[name=picture]",$dom).attr("src",msg[0].small)
								.attr("midpath",msg[0].middle)
								.attr("orgpath",msg[0].origin);
		//var content=$("div.pageFormContent");
		//$("img[name=picture]",content).attr("src",msg[0].path[0]).click(function(){
		//	var win=window.open("about:blank");
		//	win.document.body.innerHTML="<img src="+msg[0].origin+">"
		//});//.attr("oldpath",msg[0].origin);
		//$("input[name=file_id]",content).val(msg[0].file_id);
		//$("input[name=title]",content).focus();
		//alert($("input[name=file_id]",content).val());
		//alert($("img[name=picture]",content).attr("src"));
}

//复制
function copy(item,$dom){
	var toCategoryId = [];
	$.each($(item),function(i,category){
		toCategoryId.push(category.category_id);		
	});
	//var categoryId = $("tbody.list").find(":checkbox").parents("tr").attr("categoryId");
	var $list=$dom.parents("table.list").eq(0).find("tbody.list");
	var categoryId = $list.find(":checkbox").attr("categoryId");
	var entityType = $list.find(":checkbox").attr("entityType");
	var selId = getSelectedEntityId($list);
	if(selId.length<=0){
		alert("请选择需要复制的内容");
		return;
	}
	var url = entityType+"/"+categoryId+"/"+selId.join("_")+"/copyto/"+toCategoryId.join("_");
	DWZ.post(url,function(json){
		if(json.result>0)alert("复制成功");
	});
}

//移动
function move(item,$dom){
	var toCategoryId = item.category_id;
	var $list=$dom.parents("div.pageContent").find("tbody.list");
	var categoryId = $list.find(":checkbox").attr("categoryId");
	if(toCategoryId==categoryId){
		alertMsg.error("不能移动到本单位！");
		return;
	}
	var entityType = $list.find(":checkbox").attr("entityType");
	var selId = getSelectedEntityId($list);
	if(selId.length<=0){
		alertMsg.error("请选择需要移动的内容");
		return;
	}
	var url = entityType+"/"+categoryId+"/"+selId.join("_")+"/moveto/"+toCategoryId;
	DWZ.put(url,function(json){
		if(json.result>0){
			alertMsg.info("移动成功");
			$.each(selId,function(i,entityId){
				$("tr[entity=" + entityId + "]",$list).remove();
			});
		}
	});
}

//收集勾选的实体,返回一个数组
function getSelectedEntityId($list){
	var selId = [];
	var selEntity = $list.find(":checkbox[checked=true]");
	$.each(selEntity,function(i,entity){
		selId.push($(entity).val());
	});
	return selId;
}
//点击移动按钮
function clickMove(obj){
	var $list=$(obj).parents("div.pageContent").find("tbody.list");
	var selId=getSelectedEntityId($list);
	if(selId.length==0)
	{
		alertMsg.error('请选择需要移动的内容');
	}
	else{
		$(obj).next("a").click();
	}
}

//点击复制按钮
function clickCopy(obj,categoryId){
	var $list=$(obj).parents("table.list").eq(0).find("tbody.list");
	var selId=getSelectedEntityId($list);
	if(selId.length==0){
		alertMsg.error('请选择需要复制的内容');
		return false;
	}else{
		$(obj).next("a").click();
	}
}